<template>
  <a-modal
    title="新增组织"
    :width="600"
    :visible="visible"
    :confirmLoading="confirmLoading"
    @ok="handleOk"
    @cancel="handleCancel"
  >
    <a-spin :spinning="confirmLoading">
      <a-form :form="form">

        <a-form-item label="父级ID">
          <a-input v-decorator="['parentId', {initialValue: mdl.parentId}]" disabled />
        </a-form-item>

        <a-form-item label="部门名称">
          <a-input v-decorator="['name', {initialValue: mdl.name, rules:[{required: true, message: '部门名称不能为空'},{max: 10, message: '部门名称不能超过10个字'}]}]" />
        </a-form-item>

        <a-form-item label="排序">
          <a-input-number :min="0" v-decorator="['sort', { initialValue: mdl.sort, rules:[{required: true, message: '排序不能为空'}]}]" />
        </a-form-item>
      </a-form>
    </a-spin>
  </a-modal>
</template>
<script>
  import { addOrg } from '@/api/organization'
  export default {
    name: 'OrgCreateModal',
    data () {
      return {
        labelCol: {
          xs: { span: 24 },
          sm: { span: 5 }
        },
        wrapperCol: {
          xs: { span: 24 },
          sm: { span: 16 }
        },
        visible: false,
        confirmLoading: false,
        mdl: {},
        form: this.$form.createForm(this, {})
      }
    },
    beforeCreate () {

    },
    created () {

    },
    methods: {
      show (parentId) {
        this.edit({ 'parentId': parentId, 'sort': 0 })
        this.visible = true
      },
      edit (record) {
        this.mdl = Object.assign({}, record)
      },
      close () {
        this.visible = false
        // 清空更新表单参数
        this.form.resetFields();
        this.$emit('close');
      },
      handleOk () {
        const _this = this
        // 触发表单验证
        this.form.validateFields((err, values) => {
          // 验证表单没错误
          if (!err) {
            console.log('form values', values)

            _this.confirmLoading = true
            addOrg(values).then(res => {
              if (res) {
                _this.$message.success('保存成功');
                _this.$emit('ok');
              } else {
                _this.$message.warning('保存失败');
              }
            }).finally(() => {
              _this.confirmLoading = false;
              _this.close()
            });
          }
        })
      },
      handleCancel () {
        this.close()
      }

    }
  }
</script>
